<template>
  <div class="unit-exbit-null-remarks-info" @mouseup="endDrag()">
    <div :id="'tem11ScrollY' + props.id" class="unit-exbit-null-remarks-info-remarks">
      <div>
        <!-- <div class="auxiliarys-name">{{ props.name }}</div>
        <div class="auxiliarys-subTitle" v-if="props.subTitle">——{{ props.subTitle }}</div> -->
        <div v-html="props.remarks" style="text-align: left; margin-top: 0px"></div>
      </div>
    </div>
    <div
      v-if="isOverflow"
      class="solid-box"
      ref="track"
      @mousedown="startDrag($event)"
      @mousemove="draggings($event)"
      @mouseup="endDrag()"
    >
      <div class="solid-border" :style="{ backgroundColor: props.iconColor }"></div>
      <div :style="{ top: knobPosition + 'px' }" ref="knob" class="templatestyle1-scroll-box-tumb">
        <div class="square-top" :style="{ backgroundColor: props.iconColor }">
          <div class="square-top-context" :style="{ borderColor: props.iconBorderColor }"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed, onMounted, nextTick } from 'vue';
import { useProjectStore } from '@/store';
// import { imgUrl, remarks } from '@/pages/templatePage/templateLtem11/util/imgUrl';
const store = useProjectStore();
const isScrollType = ref(false);
const track = ref(null); // 拖动轨道
const knob = ref(null); // 拖动手柄
const knobPosition = ref(0); // 手柄位置
const dragging = ref(false); // 是否拖动中
const per = ref(0);
const props = defineProps({
  remarks: {
    type: String,
  },
  name: {
    type: String,
  },
  color: {
    type: String,
  },
  iconColor: {
    type: String,
  },
  iconBorderColor: {
    type: String,
  },
  subTitle: {
    type: String,
  },
  textVisible: {
    type: Boolean,
  },
  id: {
    type: Number,
  },
});

const startDrag = (event: any) => {
  if (!isScrollType.value) {
    return;
  }
  dragging.value = true;
  let arrs = event.clientY - track.value.getBoundingClientRect().top;
  // console.log('arrs', arrs);

  if (arrs > 334 - 28) {
    // console.log('if---', arrs);
    knobPosition.value = 334 - 28;
  } else {
    // console.log('else---', arrs);

    knobPosition.value = arrs;
  }
  var container = document.getElementById(`tem11ScrollY${props.id}`); // 滚动容器
  var scrollPercent = knobPosition.value / (334 - 28); // 需要滚动到的百分比
  var scrollHeight = container.scrollHeight; //总滚动高度
  var clientHeight = container.clientHeight; //滚动容器高度
  container.scrollTop = (scrollHeight - clientHeight) * scrollPercent; //设置滚动位置
};

// 拖动时更新手柄位置
const draggings = (event: any) => {
  if (!isScrollType.value) {
    return;
  }
  if (!dragging.value) return;
  const trackTop = track.value.getBoundingClientRect().top;
  const trackHeight = 334;
  const y = event.clientY - trackTop;
  let arrs = Math.min(trackHeight, Math.max(0, y));
  if (arrs > 334 - 28) {
    // console.log('if---', arrs);
    knobPosition.value = 334 - 28;
  } else {
    knobPosition.value = arrs;
  }
  // console.log('knobPosition.value ', knobPosition.value);

  var container = document.getElementById(`tem11ScrollY${props.id}`); // 滚动容器
  var scrollPercent = knobPosition.value / 334; // 需要滚动到的百分比
  var scrollHeight = container.scrollHeight; //总滚动高度
  var clientHeight = container.clientHeight; //滚动容器高度
  container.scrollTop = (scrollHeight - clientHeight) * scrollPercent; //设置滚动位置
};

// 结束拖动时更新状态
const endDrag = () => {
  dragging.value = false;
};
const isScrollBox = (element: any) => {
  return element?.scrollHeight > element?.clientHeight;
};
const initScroll = () => {
  var container = document.getElementById(`tem11ScrollY${props.id}`); // 滚动容器
  if (isScrollBox(container)) {
    isScrollType.value = true;
    container.scrollTop = 0;
    // console.log('滚动盒子开启滚动');
  } else {
    isScrollType.value = false;
    // console.log('滚动盒子开启禁止滚动');
  }
};
const initScrollBox = () => {
  // console.log('788888');

  if (!isScrollType.value) {
    return;
  }
  var container = document.getElementById(`tem11ScrollY${props.id}`); // 滚动容器
  container.addEventListener('scroll', function () {
    // console.log('789');

    var scrollTop = container.scrollTop;
    var scrollHeight = container.scrollHeight;
    var height = container.clientHeight;
    var scrolled = (scrollTop / (scrollHeight - height)) * 100;
    knobPosition.value = (224 * scrolled) / 100;
    // console.log(knobPosition.value);
    if (knobPosition.value > 224 - 28) {
      knobPosition.value = 224 - 28;
    } else {
      knobPosition.value = knobPosition.value;
    }
  });
};
const isOverflow = ref(false);
const getDom = () => {
  const container = document.getElementById(`tem11ScrollY${props.id}`);
  // console.log(container?.clientHeight);
  if (!container) {
    return;
  }
  const containerWidth = container.clientWidth;
  const containerHeight = container.clientHeight;
  const lineHeight = parseInt(getComputedStyle(container).getPropertyValue('line-height'));

  const contentWidth = container.scrollWidth;
  const contentHeight = container.scrollHeight;
  console.log('contentHeight', contentHeight, lineHeight);

  const lineCount = Math.round(contentHeight / lineHeight);
  const isContentOverflow = contentWidth > containerWidth || contentHeight > containerHeight;

  isOverflow.value = lineCount > 14 || isContentOverflow;
  // console.log('行数', lineCount);
};
onMounted(() => {
  initScroll();
  initScrollBox();
  getDom();
});
watch(
  () => props.remarks,
  () => {
    nextTick(() => {
      initScroll();
      initScrollBox();
      getDom();
    });
  },
  { deep: true },
);
watch(
  () => props.textVisible,
  () => {
    nextTick(() => {
      initScroll();
      initScrollBox();
      getDom();
    });
  },
  { deep: true },
);
</script>

<style scoped lang="less">
.unit-exbit-null-remarks-info {
  display: flex;
  height: 100%;
  max-height: 224px;
  position: relative;
  // margin-left: 24px;
  overflow: hidden;
  .unit-exbit-null-remarks-info-remarks {
    flex: 1;
    max-height: 224px;
    overflow-y: auto;
    text-indent: 2em;
    font-size: 16px;
    line-height: 32px;
    font-family: 'PingFangSC, PingFang SC';
    color: rgba(0, 0, 0, 0.7);
    // span {
    //   text-indent: 2em;
    //   font-size: 17px;
    //   font-weight: 500;

    //   color: rgba(0, 0, 0, 0.7);
    //   font-family: SourceHanSerifCN;
    //   line-height: 30px;
    //   margin-bottom: 24px;
    // }
  }
}
.solid-box {
  position: relative;
  width: 35px;
  height: 100%;
  margin-left: 24px;
  padding-top: 4px;
}
.unit-exbit-null-remarks-info-remarks::-webkit-scrollbar {
  display: none;
}
.templatestyle1-scroll-box-tumb {
  position: absolute;
  position: relative;
  z-index: 60;
}

.solid-border {
  position: absolute;
  top: 0;
  left: 10px;
  height: 100%;
  width: 1px;
  border-radius: 5px;
  opacity: 0.3;
  // background: #9d855e;
}

#tem11ScrollY {
  line-height: 32px;
}
.auxiliarys-name {
  color: #333;
  text-align: center;
  font-family: SourceHanSerifCN-Bold;
  font-size: 24px;
  font-style: normal;

  line-height: 32px; /* 133.333% */
}
.auxiliarys-subTitle {
  color: #333;
  text-align: center;
  font-family: SourceHanSerifCN;
  font-size: 18px;
  font-style: normal;
  line-height: 32px; /* 177.778% */
}
.square-top {
  width: 20px;
  height: 20px;
  background: #8c7a57;
  transform: rotate(45deg);
  border-radius: 2px;
  overflow: hidden;
}
.square-top-context {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  border: 2px solid #443c2e;
  margin: 3px;
}
</style>
